<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { useUserStore } from '@/stores/counter'
import AsideBar from './components/AsideBar.vue'
import { House, Setting, SwitchButton, Refresh } from '@element-plus/icons-vue'

const userStore = useUserStore()
const showUserMenu = ref(false)

// 处理头像点击
const handleAvatarClick = () => {
  showUserMenu.value = !showUserMenu.value
}

// 处理菜单项点击
const handleMenuClick = (action: string) => {
  showUserMenu.value = false

  switch (action) {
    case 'home':
      // 暂时不实现具体功能
      console.log('点击了主页')
      break
    case 'settings':
      // 暂时不实现具体功能
      console.log('点击了个人设置')
      break
    case 'refresh':
      // 暂时不实现具体功能
      console.log('点击了重置密码')
      break
    case 'logout':
      // 暂时不实现具体功能
      console.log('点击了退出登录')
      break

  }
}

// 点击外部关闭菜单
const handleClickOutside = (event: Event) => {
  const target = event.target as Element
  const userMenuPopup = document.querySelector('.user-menu-popup')
  const avatarWrapper = document.querySelector('.avatar-wrapper')

  if (userMenuPopup && !userMenuPopup.contains(target) &&
    avatarWrapper && !avatarWrapper.contains(target)) {
    showUserMenu.value = false
  }
}

// 获取头像
const getMessageAvatar = () => {
  return userStore.currentUser?.avatar || '/default-avatar.png'
}

// 组件挂载时添加事件监听
onMounted(() => {
  document.addEventListener('click', handleClickOutside)
})

// 组件卸载时清理事件监听
onUnmounted(() => {
  document.removeEventListener('click', handleClickOutside)
})
</script>
<template>
  <div class="common-layout">
    <el-container>
      <el-aside>
        <AsideBar @avatar-click="handleAvatarClick" />
      </el-aside>
      <el-main>
        <RouterView />
      </el-main>
    </el-container>

    <!-- 用户菜单悬浮窗 -->
    <div v-if="showUserMenu" class="user-menu-popup" @click.stop>
      <!-- 当前登录账号 -->
      <div class="user-info-section">
        <div class="section-title">当前登录账号</div>
        <div class="user-profile">
          <el-avatar :size="40" :src="getMessageAvatar()" class="profile-avatar" />
          <div class="profile-details">
            <div class="profile-nickname">{{ userStore.currentUser?.nickname || '未设置昵称' }}</div>
            <div class="profile-email">{{ userStore.currentUser?.email || '未设置邮箱' }}</div>
          </div>
        </div>
      </div>

      <!-- 菜单选项 -->
      <div class="menu-options">
        <div class="menu-item" @click="handleMenuClick('home')">
          <el-icon class="menu-icon">
            <house />
          </el-icon>
          <span class="menu-text">主页</span>
        </div>

        <div class="menu-item" @click="handleMenuClick('settings')">
          <el-icon class="menu-icon">
            <setting />
          </el-icon>
          <span class="menu-text">个人设置</span>
        </div>
        <div class="menu-item" @click="handleMenuClick('refresh')">
          <el-icon class="menu-icon">
            <Refresh />
          </el-icon>
          <span class="menu-text">重置密码</span>
        </div>
        <div class="menu-item logout-item" @click="handleMenuClick('logout')">
          <el-icon class="menu-icon">
            <switch-button />
          </el-icon>
          <span class="menu-text">退出登录</span>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.common-layout {
  height: 100vh;
  overflow: hidden;
  position: relative;

  .el-container {
    height: 100%;

    .el-aside {
      width: auto;
      padding: 0;
      flex-shrink: 0;
    }

    .el-main {
      padding: 0;
      overflow: hidden;
    }
  }

  .user-menu-popup {
    position: fixed;
    left: 100px;
    bottom: 80px;
    width: 280px;
    background: #FFFFFF;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-xl);
    border: 1px solid var(--border-light);
    backdrop-filter: blur(20px);
    z-index: 1000;
    overflow: hidden;
    animation: slideIn 0.2s ease-out;

    &::before {
      content: '';
      position: absolute;
      left: -8px;
      bottom: 24px;
      width: 0;
      height: 0;
      border-style: solid;
      border-width: 8px 8px 8px 0;
      border-color: transparent var(--gray-900) transparent transparent;
    }

    .user-info-section {
      padding: var(--spacing-lg);
      border-bottom: 1px solid var(--border-light);

      .section-title {
        font-size: var(--text-xs);
        color: var(--text-secondary);
        font-weight: var(--font-medium);
        margin-bottom: var(--spacing-md);
        text-transform: uppercase;
        letter-spacing: 0.5px;
      }

      .user-profile {
        display: flex;
        align-items: center;
        gap: var(--spacing-md);

        .profile-avatar {
          border: 2px solid var(--primary-400);
        }

        .profile-details {
          flex: 1;
          min-width: 0;

          .profile-nickname {
            font-size: var(--text-base);
            font-weight: var(--font-semibold);
            color: var(--text-primary);
            margin-bottom: 2px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }

          .profile-email {
            font-size: var(--text-sm);
            color: var(--text-secondary);
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }
        }
      }
    }

    .menu-options {
      padding: var(--spacing-sm) 0;

      .menu-item {
        display: flex;
        align-items: center;
        gap: var(--spacing-md);
        padding: var(--spacing-md) var(--spacing-lg);
        cursor: pointer;
        transition: var(--transition-fast);
        color: var(--text-primary);

        &:hover {
          background: rgba(99, 102, 241, 0.1);
          color: var(--primary-300);
        }

        .menu-icon {
          font-size: 18px;
          color: var(--text-secondary);
          transition: var(--transition-fast);
        }

        .menu-text {
          font-size: var(--text-sm);
          font-weight: var(--font-medium);
        }

        &:hover .menu-icon {
          color: var(--primary-400);
        }

        &.logout-item {
          color: var(--error-400);

          &:hover {
            background: rgba(239, 68, 68, 0.1);
            color: var(--error-300);
          }

          .menu-icon {
            color: var(--error-400);
          }

          &:hover .menu-icon {
            color: var(--error-300);
          }
        }
      }
    }
  }
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateX(-10px);
  }

  to {
    opacity: 1;
    transform: translateX(0);
  }
}
</style>
